/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.openbookformat.project;

import java.io.IOException;
import org.netbeans.api.project.Project;
import org.netbeans.spi.project.ProjectFactory;
import org.netbeans.spi.project.ProjectState;
import org.openide.filesystems.FileObject;

/**
 *
 * @author MUFEED
 */
@org.openide.util.lookup.ServiceProvider(service=ProjectFactory.class)
public class OBFProjectFactory implements ProjectFactory {
    public static final String PROJECT_FILE = "obfproject.xml";
    public static final String HEADING_FILE = "headings.properties";

    /*
     * check if the project has the project file
     */
    @Override
    public boolean isProject(FileObject projectDir) {
        return projectDir.getFileObject(PROJECT_FILE) != null;
    }

    /*
     * open the project if it exists
     */
    @Override
    public Project loadProject(FileObject projectDir, ProjectState ps) throws IOException {
        return isProject(projectDir) ? new OBFProject(projectDir, ps): null;
    }

    @Override
    public void saveProject(Project project) throws IOException, ClassCastException {
        FileObject projectRoot = project.getProjectDirectory();
        if (projectRoot.getFileObject(PROJECT_FILE) == null) {
            throw new IOException("Project file " + projectRoot.getPath() +
                    " deleted," +
                    " cannot save project");
        }
        //Force creation of the project file if it was deleted:
        ((OBFProject) project).getProjectFile(true);
    }
}
